package engine.system;

import engine.Engine;

import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;

public abstract class MADFrameRate {

	public static final int SYNC_FRAME_RATE = 100 ; 
	
	private static long lastFrame ; 
	private static int fps ; 
	private static int fpsTmp ; 
	private static long lastFPS ; 
	private static boolean canUpdate = false ; 
	
	public static int getFPS(){
		return fpsTmp ; 
	}
	
	public static void start(){
		getDelta();
		lastFPS = getTime(); 
	}
	
	public static void update(){
		getDelta();
		updateFPS(); 
	}
	
	public static int getDelta(){
		long time = getTime(); 
		int delta = (int) (time - lastFrame);
		lastFrame = time ; 
		return delta ; 
	}
	
	private static long getTime(){
		return (Sys.getTime() * 1000) / Sys.getTimerResolution(); 
	}
	
	private static void updateFPS(){
		canUpdate = getTime() - lastFPS > 1000 ; 
		if(canUpdate){
			Display.setTitle(Engine.APP_TITLE + " | Running on "+fps+" FPS"); 
			fpsTmp = fps ; 
			fps = 0 ; 
			lastFPS += 1000 ; 
		}
		fps++ ; 
	}
	
	public static boolean canUpdate(){
		return canUpdate ; 
	}
	
	public static long getLastFrame(){
		return lastFrame ; 
	}
	
	
}
